package com.gt.quality.system.entity.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.gt.quality.system.entity.mock.MockInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.BooleanTypeHandler;

import java.io.Serializable;
import java.util.Map;

/**
 * MyBatis 会自动将 is_active 列的 0 和 1 映射为 false 和 true
 */
@Schema(title = "模拟器", description = "EasyCode")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "mock_info", autoResultMap = true)
public class MockInfoRequest implements Serializable {
    /**
     * Description:  用户ID
     */
    @Schema(title = "mock_id", description = "mock_id")
    @TableId(value = "mock_id", type = IdType.ASSIGN_ID)
    private Long mockId;

    @Schema(title = "interface_router", description = "接口路径:前缀->/qa/mock/api/***")
    @TableField(value = "interface_router")
    @NotNull
    private String interfaceRouter;

    @Schema(title = "reqMethod", description = "请求方式:post/get")
    @TableField(value = "req_method")
    private String reqMethod;

    @Schema(title = "blockedTime", description = "延时返回时间-毫秒")
    @TableField(value = "blocked_time")
    private long blockedTime;

    @Schema(title = "enableBlockedTime", description = "开启延时返回")
    @TableField(value = "enable_blocked_time", typeHandler = BooleanTypeHandler.class)
    private boolean enableBlockedTime;

    @Schema(title = "responseBody", description = "响应Body")
    private Map<String,Object> responseBody;

    @Schema(title = "checkedHeader", description = "校验Header")
    private Map<String,String> checkedHeader;

    @Schema(title = "enableHeaderCheck", description = "开启校验Header")
    @TableField(value = "enable_header_check", typeHandler = BooleanTypeHandler.class)
    private boolean enableHeaderCheck;

    public MockInfo buildMockInfo(MockInfoRequest request) {
        MockInfo bean = new MockInfo();
        bean.setResponseBody(request.getResponseBody() == null ? "{}" : JSONUtil.toJsonStr(request.getResponseBody()));
        bean.setCheckedHeader(request.getCheckedHeader() == null ? "{}" : JSONUtil.toJsonStr(request.getCheckedHeader()));
        bean.setMockId(request.getMockId());
        bean.setInterfaceRouter(request.getInterfaceRouter());
        bean.setReqMethod(request.getReqMethod());
        bean.setBlockedTime(request.getBlockedTime());
        bean.setEnableBlockedTime(request.isEnableBlockedTime());
        bean.setEnableHeaderCheck(request.isEnableHeaderCheck());
        return bean;
    }

}
